;2008-05-20
;Av Benjamin Ingberg och Patrik Johansson
;
;Lyssnar på tangenter, aka "keys"

(define *key-listeners* '())

(define (handle-key-down-event key)
  (for-each
   (lambda (keylistener)
     (send keylistener set-key-down! key))
   *key-listeners*))

(define (handle-key-up-event key)
  (for-each 
   (lambda (keylistener)
     (send keylistener set-key-up! key))
   *key-listeners*))

(define key-listener%
  (class object%
    (super-new)
    (set! *key-listeners* (cons this *key-listeners*))
    (define keys '())
    (define/public (set-key-down! key)
      (let ((key-pair (assq key keys)))
        (if key-pair
            (set-cdr! key-pair #t)
            (set! keys (cons (cons key #t) keys)))))
    (define/public (set-key-up! key)
      (let ((key-pair (assq key keys)))
        (if key-pair
            (set-cdr! key-pair #f)
            (set! keys (cons (cons key #f) keys)))))
    (define/public (is-key-down? key)
      (let ((key-pair (assq key keys)))
        (if key-pair
            (cdr key-pair)
            #f)))))